import {createSlice,PayloadAction,Dispatch} from '@reduxjs/toolkit'
import {IShopcartState} from '../../types'
import $http from '../../api/http'
const initialState:IShopcartState={
    rows:[]
}
export const shopcartSlice=createSlice({
    name:'shopcart',
    initialState,
    reducers:{
        setShopcartList(state,{payload}:PayloadAction<any[]>){
            console.log('-----购物车状态机中的reducer操作-----------');
            //更新初始数据
            state.rows=payload
        }
    }
})
export const getShopcartListAsync=()=>{
    return async(dispatch:Dispatch)=>{
        console.log('-----网路请求-----------');
        //完成异步请求
        const {data:{data}}=await $http.shopcart.getShopcartList()
        dispatch(setShopcartList(data))
    }
}
export const {setShopcartList}=shopcartSlice.actions
export default shopcartSlice.reducer